package bdqn.t09.controller;
import bdqn.t09.dao.HatCityMapper;
import bdqn.t09.pojo.*;
import bdqn.t09.service.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@Controller
public class CustomsController {

    // 省
    @Resource
    private HatProvinceService provinceService;

    // 城
    @Resource
    private HatCityService cityService;

    // 区
    @Resource
    private HatAreaService areaService;

    // 配置（词典）信息
    @Resource
    private AsSystemconfigService systemconfigService;

    // 客户/公司
    @Resource
    private AsCustomsService customsService;

    // 联系人
    @Resource
    private AsContactsService contactsService;

    // 在添加客户页面跳转回客户管理界面
    @RequestMapping(value = "addCustomsReturnCustomsMag")
    public String addCustomsReturnCustomsMag(){
        return "agentCustomerManagement";
    }

    @RequestMapping(value ="/customsInsert")
    public String goInsertCustoms(Model model) {
        // 拿到省
        List<HatProvince> hatProvinces = provinceService.selectAllPrimary();
        // 拿到市
        List<HatCity> hatCity = cityService.selectAllCity();
        // 拿到区
        List<HatArea> hatAreas = areaService.selectAllArea();
        // 拿到企业类型
        List<AsSystemconfig> customtype = systemconfigService.findConfigById(5);
        // 拿到证件类型
        List<AsSystemconfig> cardtype = systemconfigService.findConfigById(6);

        // 将的到的数据用属性包装起来
        model.addAttribute("hatProvinces",hatProvinces);
        model.addAttribute("hatCity",hatCity);
        model.addAttribute("hatAreas",hatAreas);
        model.addAttribute("customtype",customtype);
        model.addAttribute("cardtype",cardtype);

//        String hh = JSON.toJSONString(customtype);
//        System.out.println(hh);

        // 转到页面
        return "insertCustoms";
    }

    // 添加客户
    @RequestMapping(value = {"/insertCustoms"},method = RequestMethod.POST)
    @ResponseBody
    public Object doInsert(AsCustoms customs){
        int custom = customsService.insertSelective(customs);
        return custom;
    }

    //    添加联系人
    @RequestMapping(value = {"/insertContact"},method = RequestMethod.POST)
    @ResponseBody
    public Object doInsertContact(@RequestBody Map map){
        List<AsContacts> asContactsList = new ArrayList<AsContacts>();

        // 联系人职务
        List<String> role = (List<String>) map.get("role");
        for (int i=0;i<role.size();i++) {
            asContactsList.add(new AsContacts());
            Random ran = new Random();
            AsContacts asRole = asContactsList.get(i);
            asRole.setCustomid((long)ran.nextInt(99));
        }
        for (int i=0;i<role.size();i++) {
            AsContacts asRole = asContactsList.get(i);
            asRole.setContactrole(role.get(i));
        }
        // 联系人姓名
        List<String> name = (List<String>) map.get("name");
        for (int i=0;i<name.size();i++) {
            AsContacts asName = asContactsList.get(i);
            asName.setContactname(name.get(i));
        }
        // 联系人电话
        List<String> tel = (List<String>) map.get("tel");
        for (int i=0;i<tel.size();i++) {
            AsContacts asTel = asContactsList.get(i);
            asTel.setContacttel(tel.get(i));
        }
        // 联系人传真
        List<String> fax = (List<String>) map.get("fax");
        for (int i=0;i<fax.size();i++) {
            AsContacts asFax = asContactsList.get(i);
            asFax.setContactfax(fax.get(i));
        }
        // 联系人邮箱
        List<String> email = (List<String>) map.get("email");
        for (int i=0;i<email.size();i++) {
            AsContacts asEmail = asContactsList.get(i);
            asEmail.setContactemail(email.get(i));
        }

        //添加联系人
        int contacts = 0;
        for (AsContacts asContacts : asContactsList) {
            contacts = contactsService.insertSelective(asContacts);
        }
        return contacts;
    }
}
